package nix.upload.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

public class Download1Servlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String fileName = "d:/a.mp3";
		// 获取文件的MIME类型
		String contentType = this.getServletContext().getMimeType(fileName);

		String destName = "中文名.mp3";
		// 简单处理方法
		// destName = new String(destName.getBytes("GBK"), "ISO-8859-1");
		// 使用编码处理工具
		destName = DownUtils.fileNameEncoding(destName, request);
		String contentDisposition = "attachment;filename=" + destName;

		FileInputStream input = new FileInputStream(fileName);
		response.setHeader("Content-Type", contentType);
		response.setHeader("Content-Disposition", contentDisposition);

		OutputStream output = response.getOutputStream();

		IOUtils.copy(input, output);
		input.close();
	}

}
